﻿using System;
using System.Web.UI.WebControls;
using WorkOrder.Business;
using WorkOrder.Model;
using System.Drawing;

namespace WorkOrder.Notice
{
    public partial class EditNotice : System.Web.UI.Page
    {
        Business_Table_Notice b_t_n = new Business_Table_Notice();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["UserID"] == null || Session["UserID"].ToString() == "")
            {
                Session.Clear();
                ClientScript.RegisterClientScriptBlock(this.GetType(), "windowhref", "<script type='text/javascript'>alert('对不起，您还没有登录系统！');window.parent.location.href='../login.aspx';</script>");
            }
            if (!Page.IsPostBack)
            {
                RadEditor1.ToolsFile = "FullSetOfTools.xml";
                ddlNoticeType.Items.Clear();
                ddlNoticeType.DataSource = b_t_n.GetNoticeType();
                ddlNoticeType.DataBind();
                ddlNoticeType.Items.Add(new ListItem("-- 请选择 --", "0"));
                ddlNoticeType.SelectedIndex = ddlNoticeType.Items.Count - 1;
                if (!CheckPermission()) return;
                if (!string.IsNullOrEmpty(Request.Params["id"]))
                {
                    Model_Table_Notice model;
                    model = b_t_n.GetOneNotice(Request.Params["id"]);
                    if (model != null)
                    {
                        hidID.Value = model.NoticeID;
                        txtTitle.Text = model.Title;
                        ddlNoticeType.SelectedIndex = ddlNoticeType.Items.IndexOf(ddlNoticeType.Items.FindByValue(model.TypeID));
                        RadEditor1.Content = model.Content;
                    }
                }
            }
        }

        private bool CheckPermission()
        {
            if (Session["UserID"] == null) return false;
            if (!new Business_Table_Permission().HasInsert(Session["UserID"].ToString(), "Table_Notice"))
            {
                Label1.Text = Label2.Text = "提示信息：对不起，您没有编辑公告的权限！";
                Label1.ForeColor = Label2.ForeColor = Color.Red;
                return false;
            }
            return true;
        }

        private void SaveNotice()
        {
            if (!CheckPermission()) return;
            if (RadEditor1.Content.Trim() == "")
            {
                Label1.Text = Label2.Text = "提示信息：请输入公告内容！";
                Label1.ForeColor = Label2.ForeColor = Color.Red;
                return;
            }
            if (b_t_n.Update(new Model_Table_Notice(hidID.Value, txtTitle.Text.Trim(), ddlNoticeType.SelectedValue, RadEditor1.Content, "", Session["UserID"].ToString())) > 0)
            {
                txtTitle.Text = "";
                ddlNoticeType.SelectedIndex = ddlNoticeType.Items.Count - 1;
                RadEditor1.Content = "";
                Label1.Text = Label2.Text = "提示信息：提交保存成功！";
                Label1.ForeColor = Label2.ForeColor = Color.Green;
            }
            else
            {
                Label1.Text = Label2.Text = "提示信息：提交保存失败！";
                Label1.ForeColor = Label2.ForeColor = Color.Red;
            }
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            SaveNotice();
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            SaveNotice();
        }
    }
}